package com.teacher.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.mapper.DataAnalysisMapper;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.dataAnalysis.*;
import com.teacher.pojo.entity.BaseEntity;
import com.teacher.pojo.entity.PerStudent;
import com.teacher.pojo.entity.StuClass;
import com.teacher.pojo.enums.items.*;
import com.teacher.pojo.vo.dataAnalysis.*;
import com.teacher.service.IDataAnalysisService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IStuClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zfm
 * @date 2021/9/8 9:53
 */
@Service
public class DataAnalysisImpl implements IDataAnalysisService {

    @Autowired
    private IPerStudentService iPerStudentService;

    @Autowired
    private IStuClassService iStuClassService;

    @Autowired
    private DataAnalysisMapper dataAnalysisMapper;

    @Autowired
    private TermMapper termMapper;

    /**
     * 证书查询
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisCertificateGetVo>> getCertificate(DataAnalysisCertificateGetDto dto) {

        List<DataAnalysisCertificateGetVo> voList = new ArrayList<>();

        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }

        voList = dataAnalysisMapper.getCertificate(dto, studentKey);
        int all = 0;
        for (DataAnalysisCertificateGetVo vo : voList) {
            all += vo.getNums();
            if (dto.getType().equals(CertificateType.ENGLISH.getCode())) {
                vo.setLevelName(EnglishType.getNameByCode(vo.getLevel()));
            } else if (dto.getType().equals(CertificateType.SPEAKING.getCode())) {
                vo.setLevelName(SpeakType.getNameByCode(vo.getLevel()));
            } else if (dto.getType().equals(CertificateType.COMPUTER.getCode())) {
                vo.setLevelName(ComputerType.getNameByCode(vo.getLevel()));
            } else if (dto.getType().equals(CertificateType.TEACHER.getCode())) {
                vo.setLevelName(TeacherType.getNameByCode(vo.getLevel()));
            }
        }

        for (DataAnalysisCertificateGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }

        return CommonResult.success(voList);
    }

    /**
     * 任职查询
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisJobGetVo>> getJob(DataAnalysisJobGetDto dto) {
        List<DataAnalysisJobGetVo> voList = new ArrayList<>();
        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        voList = dataAnalysisMapper.getJob(dto, studentKey);
        int all = 0;
        for (DataAnalysisJobGetVo dataAnalysisJobGetVo : voList) {
            all += dataAnalysisJobGetVo.getNums();
            dataAnalysisJobGetVo.setTypeName(JobType.getNameByCode(dataAnalysisJobGetVo.getType()));
        }

        for (DataAnalysisJobGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }

        return CommonResult.success(voList);
    }

    /**
     * 学科竞赛获奖
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisCourseContestsGetVo>> getCourseContests(DataAnalysisCourseContestsGetDto dto) {
        List<DataAnalysisCourseContestsGetVo> voList = new ArrayList<>();
        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        voList = dataAnalysisMapper.getCourseContests(dto, studentKey);
        int all = 0;
        for (DataAnalysisCourseContestsGetVo dataAnalysisJobGetVo : voList) {
            all += dataAnalysisJobGetVo.getNums();
            dataAnalysisJobGetVo.setTypeName(CourseContestsType.getNameByCode(dataAnalysisJobGetVo.getType()));
        }
        for (DataAnalysisCourseContestsGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }
        return CommonResult.success(voList);
    }

    /**
     * 寝室等级
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisBedroomLevelGetVo>> getBedroomLevel(DataAnalysisBedroomLevelGetDto dto) {
        List<DataAnalysisBedroomLevelGetVo> voList = new ArrayList<>();
        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        voList = dataAnalysisMapper.getBedroomLevel(dto, studentKey);
        int all = 0;
        for (DataAnalysisBedroomLevelGetVo dataAnalysisJobGetVo : voList) {
            all += dataAnalysisJobGetVo.getNums();
            dataAnalysisJobGetVo.setLevelName(BedroomLevelType.getNameByCode(dataAnalysisJobGetVo.getLevel()));
        }
        for (DataAnalysisBedroomLevelGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }
        return CommonResult.success(voList);
    }

    /**
     * 奖学金
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisRewardGetVo>> getReward(DataAnalysisRewardGetDto dto) {
        List<DataAnalysisRewardGetVo> voList = new ArrayList<>();
        if (!dto.getIsEthnic() && !dto.getIsProvince() && !dto.getIsType()) {
            return CommonResult.success(voList);
        }
        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        voList = dataAnalysisMapper.getReward(dto, studentKey);
        if (dto.getIsType()) {
            for (DataAnalysisRewardGetVo dataAnalysisRewardGetVo : voList) {
                // 10->普通学生 20->贫困生 30->特贫困生
                if (dataAnalysisRewardGetVo.getType() == 10) {
                    dataAnalysisRewardGetVo.setDescription("普通学生");
                }
                if (dataAnalysisRewardGetVo.getType() == 20) {
                    dataAnalysisRewardGetVo.setDescription("贫困生");
                }
                if (dataAnalysisRewardGetVo.getType() == 30) {
                    dataAnalysisRewardGetVo.setDescription("特贫困生");
                }
            }
        }
        int all = 0;
        for (DataAnalysisRewardGetVo dataAnalysisRewardGetVo : voList) {
            all+=dataAnalysisRewardGetVo.getNums();
        }
        for (DataAnalysisRewardGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }
        return CommonResult.success(voList);
    }

    /**
     * 文艺获奖
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<List<DataAnalysisCulturalActivitiesRewardGetVo>> getCulturalActivitiesReward(DataAnalysisCulturalActivitiesRewardGetDto dto) {
        List<DataAnalysisCulturalActivitiesRewardGetVo> voList = new ArrayList<>();
        DataAnalysisGetBaseDto baseDto = new DataAnalysisGetBaseDto();
        BeanUtils.copyProperties(dto, baseDto);
        List<String> studentKey = getStudentKeyList(baseDto);
        if (studentKey.size() == 0) {
            return CommonResult.success(voList);
        }
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }

        voList = dataAnalysisMapper.getCulturalActivitiesReward(dto, studentKey);
        int all = 0;
        for (DataAnalysisCulturalActivitiesRewardGetVo dataAnalysisCulturalActivitiesRewardGetVo : voList) {
            all += dataAnalysisCulturalActivitiesRewardGetVo.getNums();
            dataAnalysisCulturalActivitiesRewardGetVo.setTypeName(CulturalActivitiesRewardType.getNameByCode(
                    dataAnalysisCulturalActivitiesRewardGetVo.getType()
            ));
        }
        for (DataAnalysisCulturalActivitiesRewardGetVo vo : voList) {
            vo.setPercent((Float.valueOf(vo.getNums()) / all) * 100);
        }
        return CommonResult.success(voList);
    }

    /**
     * 根据类型查询学生
     *
     * @param baseDto
     * @return
     */
    private List<String> getStudentKeyList(DataAnalysisGetBaseDto baseDto) {
        List<String> studentKeyList = new ArrayList<>();
        // 按照班级查询
        if (baseDto.getIsClass()) {
            if (baseDto.getClassId() == null) {
                return studentKeyList;
            }
            // 按照班级查询
            studentKeyList = iPerStudentService.list(new QueryWrapper<PerStudent>().lambda()
                    .eq(PerStudent::getClassId, baseDto.getClassId())).stream().map(PerStudent::getStudentKey).collect(Collectors.toList());
            return studentKeyList;
        }
        // 按照专业查询
        if (baseDto.getIsDepartment()) {
            if (baseDto.getDepartmentId() == null) {
                return studentKeyList;
            }
            List<Long> classIdList = iStuClassService.list(new QueryWrapper<StuClass>().lambda()
                    .eq(StuClass::getStatus, 10)
                    .eq(StrUtil.isNotBlank(baseDto.getYear()), StuClass::getYear, baseDto.getYear())
                    .eq(StuClass::getDepartmentId, baseDto.getDepartmentId())).stream().map(BaseEntity::getId).collect(Collectors.toList());
            if (classIdList.size() > 0) {
                studentKeyList = iPerStudentService.list(new QueryWrapper<PerStudent>().lambda()
                        .in(PerStudent::getClassId, classIdList)).stream().map(PerStudent::getStudentKey).collect(Collectors.toList());
            }
            return studentKeyList;
        }
        return studentKeyList;
    }

}
